package com.gin.kotlin.no_gay_alliance.provider.ngaphp

import com.gin.kotlin.no_gay_alliance.argument.EditHistoryArgument
import com.gin.nga.main.NgaApi
import com.gin.nga.response.base.SingleData
import com.gin.nga.response.reply.EditHistory
import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit

class EditHistoryProvider private constructor() : AbstractCachedDataProvider<EditHistoryArgument, SingleData<LinkedHashMap<Int, EditHistory>>, List<EditHistory>>(
    javaType = TYPE_FACTORY.constructCollectionLikeType(List::class.java, EditHistory::class.java),
    expires = TimeUnit.MINUTES.toSeconds(5)
) {
    override val dir = "EDIT_HISTORY"

    override fun obtainRelativePath(userId: Long, params: EditHistoryArgument?) = "${params?.topicId}_${params?.replyId}_${params?.page}.json"

    override fun requestMethod(ngaApi: NgaApi, params: EditHistoryArgument?) =
        ngaApi.topicReadApi.editHistory(params?.topicId!!, params.replyId, params.page)

    override fun transform(rawData: SingleData<LinkedHashMap<Int, EditHistory>>) = rawData.data?.values?.toList() ?: listOf()

    companion object {
        private var instance: WeakReference<EditHistoryProvider> = WeakReference(EditHistoryProvider())
        fun getInstance(): EditHistoryProvider = instance.get() ?: EditHistoryProvider().also { instance = WeakReference(it) }
    }
}